﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class UIDG:SDBASE
    {
        
        
        public UIDG()
        {
          
            
        }
        public void SetUp()
        {
            TMV.SetUp(SDSA);
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
        }
        public override void LOADALL()
        {
            base.LOADALL();
            if (LMS.Length > 0)
            {
                filter = "SDKEY LIKE '%" + LMS + "'";
                rows = TBMain.Select(filter);
                if (rows.Length > 0)
                {
                    TBTemp = rows.CopyToDataTable();
                    TBMain.Clear();
                    TBMain.Merge(TBTemp);
                }
            }
            foreach (DataRow row in TBMain.Rows)
            {
                row["SDES"] = Helpers.Helper.Convert8859P1ToGB2312(row["SDES"].ToString().Trim());
                row["DES"] = Helpers.Helper.Convert8859P1ToGB2312(row["DES"].ToString().Trim());
            }
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {

                TXTISIND = "";
            }
            else
            {
                TXTISIND = (SDSA + KEY.Trim().PadRight(21) + LMS).PadRight(60);
            }

        }

        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (SDSA == null)
                return;
            if (SDSA.Trim() == "")
                return;
            TMV.SetUp(SDSA);
            dwData = TBMain.NewRow();
            dwData["SDSA"] = SDSA;
            dwData["SDKEY"] = " AND SDKEY LIKE '%" + LMS + "'";
            SResult = TMV.QuerySDDAT(dwData, System.Convert.ToInt32(SDSA));
            MergeData(SResult);
            base.QueryCommand(sender, e);
        }

        public override void NewCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (KEY.Trim().Length == 0)
            {
                MessageBox.Show("需要先键入新键值");
                return;
            }
            try
            {
                DataRow dwData;
                dwData = TBMain.NewRow();
                TMV.SetUpNewRow(ref dwData, SDSA);
                dwData["SDKEY"] = "001                    " + LMS;
                dwData["KEY"] = KEY;
                TBMain.Rows.InsertAt(dwData, 0);
                vwMaster.View.MoveCurrentToFirst();
                base.NewCommand(sender, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show("请尝试查询一下再新建");
            }
        }



        public override void DeleteCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.DeleteSDDAT(dwMaster, 1);
            string sdkey = dwMaster["SDKEY"].ToString();
            vwMaster.View.MoveCurrentTo(-1);
            string filter = "SDKEY='" + sdkey + "'";
            DataRow[] rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }

        public override void CopyCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (KEY.Trim().Length == 0)
            {
                MessageBox.Show("需要先键入新键值");
                return;
            }
            if (dwMaster == null)
                return;
            DataRow dwData;
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            dwData["KEY"] = KEY;
            TBMain.Rows.InsertAt(dwData, 0);
            vwMaster.View.MoveCurrentToFirst();
            base.CopyCommand(sender, e);
        }

        public override void SaveCommand(object sender, System.Windows.RoutedEventArgs e)
        {

            try
            {
                if (dwMaster == null)
                    return;
                TMV.Save(dwMaster);
                base.SaveCommand(sender, e);

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public void WVCommand(object sender, RoutedEventArgs e)
        {
            VICC_WFEVENTS.SVWF.WT06(RUNID, KEY);
        }


    }
}
